﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using Binding.ControlWrappers;
using Binding;
using System.ComponentModel;
using System.Windows.Input;
using Binding.ControlWrappers;
using System.Collections;

namespace UserControlExamples.ViewModels
{
    [Serializable]
    public class CustomerViewModel : INotifyPropertyChanged, IObservable
    {
        public CustomerViewModel ThisCustomerViewModel { get { return this; } }

        private Model.Customer m_Customer = new Model.Customer();
        public string CustomerName
        {
            get { return m_Customer.CustomerName; }
            set
            {
                if (m_Customer.CustomerName != value)
                {
                    m_Customer.CustomerName = value;
                    OnPropertyChanged("CustomerName");
                    NotifyObservers(this.CustomerId.ToString() + " CustomerName " + m_Customer.CustomerName);
                }
            }
        }

        public long CustomerId
        {
            get { return m_Customer.CustomerId; }
            set
            {
                if (m_Customer.CustomerId != value)
                {
                    m_Customer.CustomerId = value;
                    OnPropertyChanged("CustomerId");

                }
            }
        }

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        public virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public ICommand ViewDetails
        {
            get
            {
                return new ViewDetails();
            }
        }

        Hashtable m_lstObservers = new Hashtable();
        public void RegisterInterest(IObserver oObserver)
        {
            m_lstObservers.Add(oObserver, oObserver);
        }

        public void UnRegisterInterest(IObserver oObserver)
        {
            m_lstObservers.Remove(oObserver);
        }

        public void NotifyObservers(object oObject)
        {
            foreach (IObserver oObserver in m_lstObservers.Keys)
            {
                oObserver.NotifyObserver(oObject);
            }
        }
    }

    #region Commands
    public class ViewDetails : ICommand
    {
        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            BinderBase target = (BinderBase)parameter;
            CustomerViewModel model = (CustomerViewModel)target.DataContext;
            model.OnPropertyChanged("CustomerName");
        }

        #endregion
    }
    #endregion


}